# -*- conding:UTF-8 -*-

'''
@ Project: P8_lxpro_7.7
@ File: jwtToken_gen_che.py
@ Author: 罗婷
@ Date：2022/7/7 15:30

'''
import datetime
import random

import jwt
from flask import current_app

def generate_token(payload):
    secret = current_app.config.get('SERCRET_KEY')
    expire = datetime.datetime.now()+datetime.timedelta(hours=12)
    payload.update({'exp': expire})
    token = jwt.encode(payload, secret, algorithm='HS256')
    # print('生成的token', token)
    if token:
        return token






def verify_token(token):
    secret = current_app.config.get('SERCRET_KEY')
    payload = jwt.decode(token, secret, algorithms='HS256')
    # print('解码的payload', payload)
    return payload


# def generate_token(payload):
#     secret = current_app.config.get('SERCRET_KEY')
#     expire = f'{datetime.datetime.now() + datetime.timedelta(hours=12)}'
#     payload.update({'exp': expire})
#     token = jwt.encode(payload, secret, algorithm='HS256')
#     if token:
#         return token

# def _generate_token(payload, is_refresh=True):
#     expirt = datetime.datetime.now() + datetime.timedelta(hours=2)
#     token = generate_token(payload)
#     if is_refresh:
#         expirts = datetime.datetime.now() + datetime.timedelta(days=15)
#         token2 = generate_token(payload, expirts)
#         # payload.update({'is_refresh': True})
#     else:
#         token2 = None
#     return token, token2

# def check_token(token, algorithm='HS256'):
#     secret = current_app.config.get('SERCRET_KEY')
#     payload = jwt.decode(token, secret, algorithm)
#     if payload:
#         return payload
#     else:
#         return None

# TODO    algorithms  ssss
# def check_token(token):
#     secret = current_app.config.get('SECRET_KEY')
#     try:
#         payload = jwt.decode(token, secret, algorithms='HS256')
#         print('解码里payload~~~~~~~', payload)
#         return payload
#     except:
#         return None
#
# def verify_token(token, secret=None):
#     if not secret:
#         secret = current_app.config.get('SECRET_KEY')
#     try:
#         payload = jwt.decode(token, secret, algorithms="HS256")
#     except Exception as e:
#         print("verify_token的错误信息", e)
#         payload = None
#     return payload